﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeCamp12.Domain;
using System.Data.SqlClient;
using System.Configuration;
using log4net;

namespace CodeCamp12.Domain
{
    public class TimeSlotsDataManagement
    {
        private ConnectionStringProvider connString = new ConnectionStringProvider();
        private ILog log = LogManager.GetLogger("CodeCamp12.DAL");

        /// <summary>
        /// Inserts new TimeSlot in the database
        /// </summary>
        /// <param name="timeSlot"></param>
        public void InsertTimeSlot(TimeSlots timeSlot)
        {
            var conn = connString.GetConnection();
            var comm = new SqlCommand("InsertTimeSlot", conn) { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@StartFrom", timeSlot.From);
            comm.Parameters.AddWithValue("@EndsAt", timeSlot.To);
            comm.Parameters.AddWithValue("@EventID", timeSlot.EventID);
            comm.Parameters.AddWithValue("@Type", timeSlot.Type);
            try
            {       
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                var log = LogManager.GetLogger("CodeCamp12.DAL");
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }
        }
      
        /// <summary>
        /// Returns the time slots for event with id passed as parametar
        /// </summary>
        /// <param name="eventId"></param>
        /// <returns>TimeSlots</returns>
        public List<TimeSlots> GetTimeSlotsForEvent(Guid eventId)
        {
            List<TimeSlots> timeSlotList = null;
            var conn = connString.GetConnection();
            var comm = new SqlCommand("GetTimeSlotsForEvent") { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@eventId", eventId);
            comm.Connection = conn;
            try
            {
                conn.Open();
                var reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    if(timeSlotList == null)
                    {
                        timeSlotList = new List<TimeSlots>();
                    }
                    var timeSlot = new TimeSlots
                    {
                        ID = new Guid(reader["ID"].ToString()),
                        From = Convert.ToDateTime(reader["StartFrom"]),
                        To = Convert.ToDateTime(reader["EndsAt"]),
                        EventID = new Guid(reader["EventID"].ToString()),
                        Type = reader["Type"].ToString(),
                        Link = reader["Link"].ToString()
                    };
                    timeSlotList.Add(timeSlot);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }

            return timeSlotList;
        }

        /// <summary>
        /// Returns the time slots for one track for event
        /// </summary>
        /// <param name="trackId"></param>
        /// <param name="eventId"></param>
        /// <returns>TimeSlots</returns>
        public List<TimeSlots> GetTimeSlotsForTrack(Guid trackId, Guid eventId)
        {
            List<TimeSlots> timeSlotList = null;
            var conn = connString.GetConnection();
            var comm = new SqlCommand { CommandType = System.Data.CommandType.StoredProcedure, CommandText = "GetTimeSlotsForTrack" };
            comm.Parameters.AddWithValue("@TrackID", trackId);
            comm.Parameters.AddWithValue("@EventID", eventId);
            comm.Connection = conn;
            try
            {
                conn.Open();
                var reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    if(timeSlotList == null)
                    {
                        timeSlotList = new List<TimeSlots>();
                    }
                    var timeSlot = new TimeSlots
                    {
                        ID = new Guid(reader["ID"].ToString()),
                        From = Convert.ToDateTime(reader["StartFrom"]),
                        To = Convert.ToDateTime(reader["EndsAt"]),
                        EventID = new Guid(reader["EventID"].ToString()),
                        Type = reader["Type"].ToString(),
                        Link = reader["Link"].ToString()
                    };
                    timeSlotList.Add(timeSlot);
                }
                reader.Close();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }

            return timeSlotList;
        }

        /// <summary>
        /// Deletes time slot from the database
        /// </summary>
        /// <param name="timeSlot"></param>
        public void DeleteTimeSlot(TimeSlots timeSlot)
        {
            var conn = connString.GetConnection();
            var comm = new SqlCommand("DeleteTimeSlot", conn) { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@id", timeSlot.ID);
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }
            finally
            {
                conn.Close();
            }
            

        }

        /// <summary>
        /// Sets the visitor impressions pages for an event
        /// </summary>
        /// <param name="eventId"></param>
        /// <param name="keynoteLink"></param>
        /// <param name="breakLink"></param>
        public void UpdateTimeSlot(Guid eventId, string keynoteLink, string breakLink)
        {
            var conn = connString.GetConnection();
            var comm = new SqlCommand("UpdateTimeSlot", conn) { CommandType = System.Data.CommandType.StoredProcedure };
            comm.Parameters.AddWithValue("@eventId", eventId);
            comm.Parameters.AddWithValue("@keynote", keynoteLink);
            comm.Parameters.AddWithValue("@break", breakLink);
            try
            {
                conn.Open();
                comm.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw;
            }

            finally
            {
                conn.Close();
            }
        }
    }
}
